﻿using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Linq;

namespace DTcms.DAL
{
    /// <summary>
    /// 数据库访问层
    /// </summary>
    public partial class manager_role_value : DapperRepository<Model.manager_role_value>
    {
        public manager_role_value()
        {

        }

        #region 扩展方法================================
        /// <summary>
        /// 根据管理角色表id删除数据（事务）
        /// </summary>
        /// <param name="role_id"></param>
        /// <returns>True or False</returns>
        public void Delete(IDbConnection conn, IDbTransaction trans, List<Model.manager_role_value> models, int role_id)
        {
            if (models == null)
            {
                WriteDataBase.Execute(conn, trans, "delete from " + databaseprefix + "manager_role_value where role_id=@0", role_id);//删除全部
                return;
            }
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select id,role_id from " + databaseprefix + "manager_role_value");
            strSql.Append(" where role_id="+ role_id);
            DataSet ds = ReadDataBase.QueryFillDataSet(strSql.ToString());
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                var model = models.Find(p => p.id == int.Parse(dr["id"].ToString()));
                if (model == null)
                {
                    WriteDataBase.Execute(conn, trans, "delete from " + databaseprefix + "manager_role_value where id=" + dr["id"].ToString()); //删除该字段
                }
            }
        }

        /// <summary>
        /// 根据管理角色表id获取实体类列表
        /// </summary>
        /// <param name="role_id"></param>
        /// <returns>List<Model.manager_role_value></returns>
        public List<Model.manager_role_value> GetModelList(int role_id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * from " + databaseprefix + "manager_role_value");
            strSql.Append(" where role_id=@0");
            return GetModelList(strSql.ToString(), role_id);
        }
        #endregion
    }
}
